package com.metacube.questionbank.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.metacube.questionbank.dao.AnswerDAO;
import com.metacube.questionbank.dao.CommentDAO;
import com.metacube.questionbank.dao.QuestionDAO;
import com.metacube.questionbank.dao.RecentQuestionsDAO;
import com.metacube.questionbank.dao.UserDAO;
import com.metacube.questionbank.dto.AnswerDTO;
import com.metacube.questionbank.dto.CommentDTO;
import com.metacube.questionbank.dto.QuestionDTO;
import com.metacube.questionbank.dto.UserDTO;

public class Utility {
	
	@Autowired
	QuestionDAO qDAO;
	
	@Autowired
	AnswerDAO aDAO;
	
	@Autowired
	UserDAO userDAO;
	
	@Autowired
	RecentQuestionsDAO rqDAO;
	
	@Autowired
	CommentDAO commentDAO;
	
	public Date getDate() {
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		dateFormat.format(date);
		return date;
	}

	public boolean initQuestion(QuestionDTO qDTO, int userId) {
		
		if(qDTO.getqBody().isEmpty()) {
			
			qDTO.setqBody("");
		}
		
		qDTO.setqAskedDate(getDate());
		
		qDTO.setModifiedDate(getDate());	
		
		qDTO.setUserId(userId);

		if(qDAO.insertQuestion(qDTO)==1) {
		
			return true;
		
		} else {
		
			return false;
		}
		
	}

	public boolean initAnswer(AnswerDTO aDTO, int qId, int userId) {
		
		aDTO.setAnsweredDate(getDate());
		
		aDTO.setModifiedDate(getDate());
		
		aDTO.setUserId(userId);
		
		aDTO.setqId(qId);
		
		if(aDAO.insertAnswer(aDTO)==1) {
			
			return true;
			
		} else {
		
			return false;
		}
	}
	
	
	public List<QuestionDTO> getRecentQuestions() {
		
		return qDAO.getRecentQuestions();
	}
	
	public List<QuestionDTO> getMyQuestions(int userId) {

		return qDAO.getMyQuestionsFromDB(userId);
	}

	public List<AnswerDTO> getMyAnswers(int id) {

		return aDAO.getMyAnswersFromDB(id);
	}

	public List<QuestionDTO> getUnansweredQuestions() {

		return qDAO.getUnansweredQuestionsFromDB();
	}

	public List<UserDTO> retrieveProfile(int userId) {
		
		return userDAO.retrieveProfileFromDB(userId);
	}

	public List<QuestionDTO> getQuestionForAnswer(int qId) {
		
		return qDAO.getQuestionAndAnswerFromDB(qId);
		
	}
	
	public List<AnswerDTO> getAnswers(int id) {

		return aDAO.getAnswersFromDB(id);
	}
	
public boolean inItComment(CommentDTO commentDTO, String eMail) {
		
		commentDTO.setCommentDate(getDate());
			
		commentDTO.setUserId(userDAO.getUserId(eMail));
		
				
		if(commentDAO.insertComment(commentDTO)==1) {
			
			return true;
			
		} else {
			return false;
		}
	}
public boolean inItAnswerComment(CommentDTO commentDTO, String eMail) {
	
	commentDTO.setCommentDate(getDate());
		
	commentDTO.setUserId(userDAO.getUserId(eMail));
	
			
	if(commentDAO.insertAnswerComment(commentDTO)==1) {
		
		return true;
		
	} else {
		return false;
	}
}
}
